package com.wiz.web.manager.fatory;

import org.springframework.stereotype.Service;

import com.wiz.common.XlConstant;
import com.wiz.vo.criteria.PayCriteria;
import com.wiz.vo.to.xunlian.XlBaseData;
import com.wiz.vo.to.xunlian.XlPayReqData;
import com.wiz.vo.type.PayMethodType;

/**
 * 获取讯联支付数据模板
 * @author Hakeny
 */
@Service("xlDataFactory")
public class XlDataFactory implements XlConstant{
	
	/**
	 * 根据支付方式不同选择不同data模板
	 * @param criteria
	 * @param type
	 * @return
	 */
	public XlBaseData getDataByType(PayCriteria criteria,int type,int channelType){
		if(PayMethodType.BAR_PAY.getId() == type){//商户代理商的条码支付
			return getScanPayReqData(criteria,channelType);
		}else if(PayMethodType.QUERY.getId() == type){
			return getQueryReqData(criteria,channelType);
		}else if(PayMethodType.REVERSE.getId() == type){
			return getReverseReqData(criteria,channelType);
		}else if(PayMethodType.REFUND.getId() == type){
			return getRefundReqData(criteria,channelType);
		}
		return null;
	}
	
	/**
	 * 获取扫码支付参数
	 * @param criteria
	 * @return
	 */
	private XlPayReqData getScanPayReqData(PayCriteria criteria,int channelType){
		XlPayReqData data = new XlPayReqData(channelType);
		data.setOrderNum(criteria.getOutTradeNo());
		data.setBusicd(XL_PURC_TYPE);
		data.setInscd(XL_INSCD);
		data.setMchntid(criteria.getSubMchId());
		data.setTerminalid(criteria.getTerminalId());
		data.setTxamt(String.format("%012d", criteria.getTotalFee()));
		data.setScanCodeId(criteria.getAuthCode());
		data.setSign(data.toSign(criteria.getSignKey()));//把签名数据设置到Sign这个属性中
		return data;
	}
	
	/**
	 * 获取扫码查询
	 * @param criteria
	 * @return
	 */
	private XlPayReqData getQueryReqData(PayCriteria criteria,int channelType){
		XlPayReqData data = new XlPayReqData(channelType);
		data.setOrigOrderNum(criteria.getOutTradeNo());
		data.setBusicd(XL_INQY_TYPE);
		data.setInscd(XL_INSCD);
		data.setMchntid(criteria.getSubMchId());
		data.setTerminalid(criteria.getTerminalId());
		data.setSign(data.toSign(criteria.getSignKey()));//把签名数据设置到Sign这个属性中
		return data;
	}
	
	
	/**
	 * 获取取消扫码支付
	 * @param criteria
	 * @return
	 */
	private XlPayReqData getReverseReqData(PayCriteria criteria,int channelType){
		XlPayReqData data = new XlPayReqData(channelType);
		data.setOrderNum(criteria.getOutRefundNo());
		data.setOrigOrderNum(criteria.getOutTradeNo());
		data.setBusicd(XL_VOID_TYPE);
		data.setInscd(XL_INSCD);
		data.setMchntid(criteria.getSubMchId());
		data.setTerminalid(criteria.getTerminalId());
		data.setSign(data.toSign(criteria.getSignKey()));//把签名数据设置到Sign这个属性中
		return data;
	}
	
	private XlPayReqData getRefundReqData(PayCriteria criteria,int channelType){
		XlPayReqData data = new XlPayReqData(channelType);
		data.setOrderNum(criteria.getOutRefundNo());
		data.setOrigOrderNum(criteria.getOutTradeNo());
		data.setBusicd(XL_CANC_TYPE);
		data.setInscd(XL_INSCD);
		data.setMchntid(criteria.getSubMchId());
		data.setTerminalid(criteria.getTerminalId());
		data.setSign(data.toSign(criteria.getSignKey()));//把签名数据设置到Sign这个属性中
		return data;
	}
}
